package org.mspring.platform.search.listener;

import java.util.HashMap;
import java.util.Map;

import org.hibernate.event.spi.PostInsertEvent;
import org.hibernate.event.spi.PostInsertEventListener;
import org.hibernate.persister.entity.EntityPersister;
import org.mspring.platform.search.task.LuceneAddTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Gao Youbo
 * @since 2013年7月31日
 */
@Component
public class LuceneInsertEventListener implements PostInsertEventListener {

    private static final long serialVersionUID = -6575797700380176701L;

    private static final Logger log = LoggerFactory.getLogger(LuceneInsertEventListener.class);

    @Autowired
    private LuceneAddTask luceneAddTask;

    @Override
    public void onPostInsert(PostInsertEvent event) {
        Map<Object, Object> context = new HashMap<>();
        context.put("searchable", event.getEntity());
        try {
            luceneAddTask.doAsyncTask(context);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    @Override
    public boolean requiresPostCommitHanding(EntityPersister arg0) {
        return false;
    }

}
